<?php

namespace App\Http\Validate\Kms\SelfCheck;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Kms\Home\HomeService;
use App\Service\Kms\Task\UserRoleService;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SelfCheckValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * 自检数据统计看板 - 数据统计
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function groupStatusStatisticalValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "role",
                "regionSelectionType",
                "region",
                "regionValue",
                "checkType",
                "startDate",
                "endDate",
                "activeDaysShop"
            ],
            $params,
            [
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                ),
                "startDate"           => "required_with:endDate|date_format:Y-m-d",
                "endDate"             => "required_with:startDate|date_format:Y-m-d",
                "checkType"           => "string",
                "activeDaysShop"      => "sometimes|int|min:0|max:365"
            ]
        );
    }

    /**
     * 自检数据统计看板 - 门店列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function groupStatusStatisticalShopValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "role",
                "regionSelectionType",
                "region",
                "regionValue",
                "checkType",
                "startDate",
                "endDate",
                "activeDaysShop",
                "status",
                "pageNum",
                "pageSize",
                "sortField",
                "sortOrder"
            ],
            $params,
            [
                "pageNum"             => "required|int|min:1",
                "pageSize"            => "required|int|min:1",
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                ),
                "startDate"           => "required_with:endDate|date_format:Y-m-d",
                "endDate"             => "required_with:startDate|date_format:Y-m-d",
                "checkType"           => "string",
                "activeDaysShop"      => "sometimes|int|min:0|max:365",
                "status"              => [
                    "required",
                    "int",
                    Rule::in([
                        0,
                        CruiseCheckSelfDetailModel::STATUS_1,
                        CruiseCheckSelfDetailModel::STATUS_3,
                        CruiseCheckSelfDetailModel::STATUS_4,
                        CruiseCheckSelfDetailModel::STATUS_9,
                    ])
                ],
                "sortField"           => [
                    "sometimes",
                    "string",
                    Rule::in([
                        "status_1_count",
                        "status_2_count",
                        "status_3_count",
                        "status_4_count",
                        "status_9_count",
                        "status_10_count",
                        "reviewedRate"
                    ])
                ],
                "sortOrder"           => [
                    "required_with:sortField",
                    "string",
                    Rule::in(["descend", "ascend"])
                ]
            ]
        );
    }

    /**
     * 自检数据统计看板 - 报告列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function selfCheckReportListsValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "count",
                "pageSize",
                "role",
                "regionSelectionType",
                "region",
                "regionValue",
                "checkType",
                "startDate",
                "endDate",
                "activeDaysShop",
                "status",
                "shopId"
            ],
            $params,
            array_merge([
                "count"         => "required|int|min:0",
                "pageSize"       => "required|int|min:1",
                "role"           => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "startDate"      => "required_with:endDate|date_format:Y-m-d",
                "endDate"        => "required_with:startDate|date_format:Y-m-d",
                "checkType"      => "string",
                "activeDaysShop" => "sometimes|int|min:0|max:365",
                "status"         => "string",
                "shopId"         => [
                    "sometimes",
                    "int",
                    "gt:0"
                ]
            ], ($params["shopId"] ?? 0) ? [] : [
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                )
            ])
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function shopListsValidate(array $params)
    {
        return self::getInstance()->validate(
            ["keywords"],
            $params,
            [
                "keywords" => ["required", "string"]
            ]
        );
    }

    /**
     * 自检基础信息详情
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function selfCheckDetailValidate(array $params)
    {
        return self::getInstance()->validate(
            ["cruiseOrderId"],
            $params,
            [
                "cruiseOrderId" => [
                    "required",
                    "int",
                    Rule::exists((new CruiseCheckSelfDetailModel())->getTable(), "cruiseOrderId")
                        ->where("templateType", CruiseTemplateModel::TYPE_8)
                ]
            ]
        );
    }
}
